//格式化时间
function dateFtt(fmt,date)   
{ //author: meizz   
  var o = {   
    "M+" : date.getMonth()+1,                 //月份   
    "d+" : date.getDate(),                    //日   
    "h+" : date.getHours(),                   //小时   
    "m+" : date.getMinutes(),                 //分   
    "s+" : date.getSeconds(),                 //秒   
    "q+" : Math.floor((date.getMonth()+3)/3), //季度   
    "S"  : date.getMilliseconds()             //毫秒   
  };   
  if(/(y+)/.test(fmt))   
    fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length));   
  for(var k in o)   
    if(new RegExp("("+ k +")").test(fmt))   
  fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));   
  return fmt;   
} 
function formatSubscribeTime(cellValue, options, rowObject) {
	var dateStr = "";
	if(cellValue){
		var crtTime = new Date(cellValue);
		dateStr = dateFtt("yyyy-MM-dd hh:mm:ss",crtTime);
	}
    return dateStr;
}
//上报类型字典
function typeFormate(value, options, row){
    //sex为自定义字典项
    var statusObj = vm.typeObject.reportType;
    for( var i=0;i< statusObj.length;i++ ){
        if(statusObj[i].value == value){
            return '<span>'+statusObj[i].text+'</span>' ;
        }
    }
}
//上报状态字典
function stateFormate(value, options, row){
    //sex为自定义字典项
	var statusObj = vm.stateObject.reportState;
    for( var i=0;i< statusObj.length;i++ ){
        if(statusObj[i].value == value){
            return '<span>'+statusObj[i].text+'</span>' ;
        }
    }
}

//查看详情,打开标签
function reportInfoDetail(reportInfoId){
    //先关闭菜单实现刷新效果
    parent.closeTabByPageId("999901");
    //id为菜单ID，请从菜单管理中查找，如非配置的菜单则设定个比较大的数为ID，以免与系统菜单冲突。
    parent.addTabs({id:'999901',title: '查看详情',close: true,url: 'modules/recorderReport/reportinfoDetail.html?reportInfoId='+reportInfoId ,urlType: 'relative'});
}

//重置违章图片
function resetPic(){
    $("#photo_01").attr("src", "/ehualu-admin/statics/images/init.png");
    $("#photo_li_01").attr("data-src", "/ehualu-admin/statics/images/init.png");
    $("#photo_li_01").attr("data-sub-html", "<h4>暂无违章图片</h4>");
    $("#photo_02").attr("src", "/ehualu-admin/statics/images/init.png");
    $("#photo_li_02").attr("data-src", "/ehualu-admin/statics/images/init.png");
    $("#photo_li_02").attr("data-sub-html", "<h4>暂无违章图片</h4>");
    $("#photo_03").attr("src", "/ehualu-admin/statics/images/init.png");
    $("#photo_li_03").attr("data-src", "/ehualu-admin/statics/images/init.png");
    $("#photo_li_03").attr("data-sub-html", "<h4>暂无违章图片</h4>");

    $("#_o_ys_url_1").attr("data-img-src",'${cxt}/static/images/init.png');
    $("#_o_ys_url_2").attr("data-img-src",'${cxt}/static/images/init.png');
    $("#_o_ys_url_3").attr("data-img-src",'${cxt}/static/images/init.png');
    $("#_o_ys_url_4").attr("data-img-src",'${cxt}/static/images/init.png');
    $("#_o_ys_url_5").attr("data-img-src",'${cxt}/static/images/init.png');
    $("#_o_ys_url_6").attr("data-img-src",'${cxt}/static/images/init.png');
    $("#_o_ys_url_1").attr("value", '1');
    $("#_o_ys_url_2").attr("value", '2');
    $("#_o_ys_url_3").attr("value", '3');
    $("#_o_ys_url_4").attr("value", '4');
    $("#_o_ys_url_5").attr("value", '5');
    $("#_o_ys_url_6").attr("value", '6');
    $("#_i_ys_img_1").attr("src", '${cxt}/static/images/init.png');
    $("#_i_ys_img_2").attr("src", '${cxt}/static/images/init.png');
    $("#_i_ys_img_3").attr("src", '${cxt}/static/images/init.png');
    $("#_i_ys_img_4").attr("src", '${cxt}/static/images/init.png');
    $("#_i_ys_img_5").attr("src", '${cxt}/static/images/init.png');
    $("#_i_ys_img_6").attr("src", '${cxt}/static/images/init.png');
}


$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'recorderReport/reportinfo/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id_', width: 50, key: true, hidden: true },
			{ label: '上报时间', name: 'time', index: 'time', width: 80 , formatter: formatSubscribeTime}
        ],
		viewrecords: true,
        height: 472,
        rowNum: 50,
		// rowList : [50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: false,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        },
        //点击列触发
        onSelectRow:function (id) {
            vm.selectRecordsOrNot = true;
            vm.getInfo(id);

        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			type:'',
			state:'',
			address:'',
			userName:''
		},
        typeObject:{}, //类型集合
		stateObject:{}, //状态集合
        plateNumberTypeObject:{}, //号牌种类集合
        plateNumberAreaObject:{},//车牌地区集合 e.g. 鲁A
        directionObject:{},//行驶方向集合
        laneObject:{},//行驶车道集合
        illegalTypeObject:{},//违法类型集合
        vehicleTypeObject:{},//车辆类型集合
        vehicleColorObject:{},//车辆颜色集合

        showList: true,
		title: null,
		reportInfo: {
		    location:''
        },

        selectRecordsOrNot: false,  //是否选择右侧信息列表记录

        //违法路段弹框搜索
        illegalLocationQuery:{
		    street:''
        }
	},
    mounted:function(){
        this.$nextTick(function(){ this.initSysDict()});
    },
	methods: {
        initSysDict:function() {
            $.get(baseURL + "sys/dict/select/reportType", function (r) {
                if (r.code === 0) {
                    vm.typeObject = r.dict;
                } else {
                    console.log(r.msg);
                }
            });
            $.get(baseURL + "sys/dict/select/reportState", function (r) {
                if (r.code === 0) {
                    vm.stateObject = r.dict;
                } else {
                    console.log(r.msg);
                }
            });
            $.get(baseURL + "sys/dict/select/plateNumberType", function (r) {
                if (r.code === 0) {
                    vm.plateNumberTypeObject = r.dict;
                } else {
                    console.log(r.msg);
                }
            });
            $.get(baseURL + "sys/dict/select/plateNumberArea", function (r) {
                if (r.code === 0) {
                    vm.plateNumberAreaObject = r.dict;
                } else {
                    console.log(r.msg);
                }
            });
            $.get(baseURL + "sys/dict/select/moveDirection", function (r) {
                if (r.code === 0) {
                    vm.directionObject = r.dict;
                } else {
                    console.log(r.msg);
                }
            });
            $.get(baseURL + "sys/dict/select/moveLane", function (r) {
                if (r.code === 0) {
                    vm.laneObject = r.dict;
                } else {
                    console.log(r.msg);
                }
            });
            $.get(baseURL + "sys/dict/select/illegalType", function (r) {
                if (r.code === 0) {
                    vm.illegalTypeObject = r.dict;
                } else {
                    console.log(r.msg);
                }
            });
            $.get(baseURL + "sys/dict/select/vehicleType", function (r) {
                if (r.code === 0) {
                    vm.vehicleTypeObject = r.dict;
                } else {
                    console.log(r.msg);
                }
            });
            $.get(baseURL + "sys/dict/select/carColor", function (r) {
                if (r.code === 0) {
                    vm.vehicleColorObject = r.dict;
                } else {
                    console.log(r.msg);
                }
            });
        },
		query: function () {
			vm.reload();
		},
        reset: function(){
            vm.q.type = '';
            vm.q.state = '';
            vm.q.address = '';
            vm.q.userName = '';
        },
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.reportInfo = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.reportInfo.id == null ? "recorderReport/reportinfo/save" : "recorderReport/reportinfo/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.reportInfo),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "recorderReport/reportinfo/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "recorderReport/reportinfo/info/"+id, function(r){
                vm.reportInfo = r.reportInfo;
                vm.reportInfo.time = dateFtt("yyyy-MM-dd hh:mm:ss",new Date(vm.reportInfo.time));

                //加载视频播放器
                // var _h_video = Ext.get("video_play").getHeight();
                var h_video =  $("#video_play").height();
                video_play_time = 0;
                var so = new SWFObject("${request.contextPath}/static/tools/player.swf",
                    "myCuPlayer", "100%", h_video, "9", "#000000");
                so.addParam("allowfullscreen", "true");
                so.addParam("allowscriptaccess", "always");
                so.addParam("wmode", "opaque");
                so.addParam("quality", "high");
                so.addParam("salign", "lt");
                //so.addParam("onkeydown", "enterSubmit(this,event)");
                //播放器设置文件-----------------------------
                so.addVariable("JcScpFile", "${request.contextPath}/static/tools/CuSunV4set.xml");
                so.addVariable("JcScpAutoPlay", "no"); //是否自动播放
                so.addVariable("JcScpVideoPath", vm.reportInfo.uploadVideo); //视频文件地址
                //-----------------------------------------
                so.write("CuPlayer");
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                page:page
            }).trigger("reloadGrid");
		},

        //获得车辆基本信息
        getVehicleInfo: function(){
            if (null == vm.reportInfo.illegalPlateType||"" == vm.reportInfo.illegalPlateType) {
                alert("请选择号牌种类！");
                return;
            }
            if (null == vm.reportInfo.illegalPlateArea || "" == vm.reportInfo.illegalPlateArea || vm.reportInfo.illegalPlateArea.length !=2) {
                alert("违法车牌号输入错误！");
                return;
            }
            if (null == vm.reportInfo.illegalPlateNumber || "" == vm.reportInfo.illegalPlateNumber || vm.reportInfo.illegalPlateNumber.length !=5) {
                alert("违法车牌号输入错误！");
                return;
            }
            if (vm.reportInfo.illegalPlateType.length == 1) {
                vm.reportInfo.illegalPlateType = "0" + vm.reportInfo.illegalPlateType;
            }
            var vehicleNumber = vm.reportInfo.illegalPlateArea + vm.reportInfo.illegalPlateNumber;

            //获取信息-----------





            },

        //弹出地图定位窗口
        showMapView: function() {
            if (!vm.selectRecordsOrNot) {
                alert("请选择信息列表一条记录");
                return;
            }
            console.log(vm.reportInfo.lon + "---" + vm.reportInfo.lat + "---" + vm.reportInfo.plateNumber);
            // addMarker(vm.reportInfo.lon, vm.reportInfo.lat, vm.reportInfo.plateNumber);
            $('#report_location_map_Modal').modal('show');

        },

        //弹出违法路段选择窗口
        illegalLocationList: function () {

            $('#illegal_location_select_Modal').modal({
                keyboard: true
            });

            $("#jqGrid_illegalLocationList").jqGrid({
                url: baseURL + 'recorderReport/reportAudit/listTBaseLocations',
                datatype: "json",
                colModel: [
                    { label: 'id', name: 'id', index: 'id', key: true, hidden: true },
                    { label: '地点名称', name: 'ddmc', index: 'ddmc',width:' 600px'}
                ],
                viewrecords: true,
                height: 385,
                rowNum: 50,
                rownumbers: true,
                rownumWidth: 25,
                autowidth:true,
                multiselect: false,
                pager: "#jqGridPager_illegalLocationList",
                jsonReader : {
                    root: "page.list",
                    page: "page.currPage",
                    total: "page.totalPage",
                    records: "page.totalCount"
                },
                prmNames : {
                    page:"page",
                    rows:"limit",
                    order: "order"
                },
                gridComplete:function(){
                    //隐藏grid底部滚动条
                    $("#jqGrid_illegalLocationList").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
                },
                ondblClickRow: function (rowid) {
                    var rowData = $("#jqGrid_illegalLocationList").jqGrid('getRowData',rowid);
                    vm.reportInfo.location = rowData.ddmc;
                    console.log("ddmc-----"+rowData.ddmc);
                    console.log("vm.reportInfo.location"+vm.reportInfo.location);

                    $('#illegal_location_select_Modal').modal('hide');
                }
            });
        },

        queryillegalLocation:function () {
            var page = $("#jqGrid_illegalLocationList").jqGrid('getGridParam','page');
            $("#jqGrid_illegalLocationList").jqGrid('setGridParam',{
                postData:{
                    'street': vm.illegalLocationQuery.street
                },
                page:page
            }).trigger("reloadGrid");
        },
        
        //修改上报信息保存
        reportAuditSave: function () {
            //违法时才强制上传照片
            if (vm.reportInfo.type == '1'){
                var img_01 = $("#photo_01").attr("src");
                if (img_01 == "/ehualu-admin/statics/images/init.png") {
                    alert("违章图片1未确认");
                    return;
                }
                var img_02 = $("#photo_02").attr("src");
                if (img_02 == "/ehualu-admin/statics/images/init.png") {
                    alert("违章图片2未确认");
                    return;
                }
                var img_03 = $("#photo_03").attr("src");
                if (img_03 == "/ehualu-admin/statics/images/init.png") {
                    alert("违章图片3未确认");
                    return;
                }
            }

            if (!vm.blankConfirm()) {
                return;
            }

            $.ajax({
                type: "POST",
                url: baseURL + "recorderReport/reportAudit/updateReportAuditSave",
                contentType: "application/json",
                data: JSON.stringify(vm.reportInfo),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            vm.reload();
                            vm.reportInfo = {location:''};
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });


        },


        //判空
        blankConfirm: function () {
            if (!vm.reportInfo.location) {
                alert('请选择违法地点!');
                return false;
            }
            if (!vm.reportInfo.alarmDesc) {
                alert('请输入报警内容!');
                return false;
            }




            if (!vm.reportInfo.eventType) {
                alert('请选择号牌种类!');
                return false;
            }
            if (!vm.reportInfo.eventType) {
                alert('请选择违法车牌!');
                return false;
            }
            if (!vm.reportInfo.eventType) {
                alert('请输入违法车牌号码!');
                return false;
            }
            if (!vm.reportInfo.eventType) {
                alert('请选择行驶方向!');
                return false;
            }
            if (!vm.reportInfo.eventType) {
                alert('请选择行驶车道!');
                return false;
            }
            if (!vm.reportInfo.eventType) {
                alert('请选择违法类型!');
                return false;
            }
            if (!vm.reportInfo.eventType) {
                alert('请输入车辆品牌!');
                return false;
            }
            if (!vm.reportInfo.eventType) {
                alert('请选择车辆类型!');
                return false;
            }
            if (!vm.reportInfo.eventType) {
                alert('请选择车辆颜色!');
                return false;
            }
            if (!vm.reportInfo.eventType) {
                alert('请输入车辆型号!');
                return false;
            }
            if (!vm.reportInfo.eventType) {
                alert('请输入车辆所有人!');
                return false;
            }
        }
        
	}
});















